package com.stevenmoon.csdnplus.fragment;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.widget.ImageView;

import com.dd.processbutton.iml.ActionProcessButton;
import com.mikepenz.iconics.IconicsDrawable;
import com.mikepenz.material_design_iconic_typeface_library.MaterialDesignIconic;
import com.rengwuxian.materialedittext.MaterialEditText;
import com.stevenmoon.csdnplus.R;
import com.stevenmoon.csdnplus.activity.MainActivity;
import com.stevenmoon.csdnplus.bean.LoginReqBean;
import com.stevenmoon.csdnplus.fragment.base.MvpBaseFragment;
import com.stevenmoon.csdnplus.presenter.LoginPresenter;
import com.stevenmoon.csdnplus.util.TipUtil;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by WangJun on 2016/4/7.
 */
public class LoginFragment extends MvpBaseFragment<LoginFragment, LoginPresenter> {

    @Bind(R.id.logoIV)
    ImageView mLogoIV;
    @Bind(R.id.usernameET)
    MaterialEditText mUsernameET;
    @Bind(R.id.pwdET)
    MaterialEditText mPwdET;
    @Bind(R.id.loginBtn)
    ActionProcessButton mLoginBtn;

    public static LoginFragment newInstance() {
        return new LoginFragment();
    }


    @Override
    protected void initViews() {
        initET();
    }

    private void initET() {
        Drawable accountImg = new IconicsDrawable(getActivity())
                .icon(MaterialDesignIconic.Icon.gmi_account)
                .sizeDp(24);
        Drawable pwdImg = new IconicsDrawable(getActivity())
                .icon(MaterialDesignIconic.Icon.gmi_lock)
                .sizeDp(24);
        mUsernameET.setIconLeft(accountImg);
        mPwdET.setIconLeft(pwdImg);
    }

    @OnClick(R.id.loginBtn)
    void loginClick(){
        if(checkInputValid()){
            mLoginBtn.setProgress(50);
            presenter.doLogin(
                    new LoginReqBean(mUsernameET.getText().toString(),mPwdET.getText().toString()));
        }

    }

    private boolean checkInputValid(){
        //TODO 验证输入合法
        return true;
    }

    public void showError(String msg){
        TipUtil.showToast(msg);
       // mLoginBtn.setErrorText(msg);

        mLoginBtn.setProgress(-1);
        //TODO 需要时间来显示这个转换的过程
        mLoginBtn.setProgress(0);
        mLoginBtn.setEnabled(true);
    }

    public void showLoading(){
        mLoginBtn.setProgress(50);
        mLoginBtn.setEnabled(false);
    }

    public void onLoginSuccess(){
        TipUtil.showToast("登录成功");
        mLoginBtn.setProgress(100);
        mLoginBtn.setEnabled(true);

        Intent intent = new Intent(getActivity(), MainActivity.class);
        startActivity(intent);
        getActivity().finish();
    }



    @Override
    protected int getContentViewId() {
        return R.layout.fragment_login;
    }

    @NonNull
    @Override
    public LoginPresenter createPresenter() {
        return new LoginPresenter();
    }

}
